package com.bigshark.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigshark.api.annotation.Login;
import com.bigshark.api.annotation.LoginUser;
import com.bigshark.api.model.Barrage;
import com.bigshark.api.model.User;
import com.bigshark.api.service.BarrageService;
import com.bigshark.common.utills.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/barrage")
public class BarrageController {
    @Autowired
    private BarrageService barrageSerivce;

    @Login
    @PostMapping("/")
    @ApiOperation(value = "添加弹幕",notes = "添加弹幕",tags = "弹幕接口",httpMethod = "POST")
    public R createBarrage(String  text,String token, @ApiIgnore @LoginUser User user){
        try{
            Assert.notNull(user,"请登陆");
            Barrage barrage = new Barrage();
            barrage.setUserId(user.getId());
            barrage.setContent(text);
            barrage.setAddTime(new Date());
            barrageSerivce.save(barrage);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    @GetMapping("/")
    @ApiOperation(value = "获取100条最新弹幕",notes = "获取100条最新弹幕",tags = "弹幕接口",httpMethod = "GET")
    public R queryBarrage(Long num){
        List<Barrage> data = barrageSerivce.list(new QueryWrapper<Barrage>().last("limit "+ num));
        return R.ok().put("data",data);
    }

}
